package com.promise.jokerdream.ui.drawer

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.windowInsetsPadding
import com.promise.jokerdream.R
import com.promise.jokerdream.database.account.UserAccount
import com.promise.jokerdream.ui.components.UserAvatar
import com.promise.jokerdream.ui.theme.AppFontSizes

/**
 * 导航抽屉内容组件
 */
@Composable
fun NavigationDrawerContent(
    onItemClick: (String) -> Unit,
    currentAccount: UserAccount? = null,
    isIncognitoEnabled: Boolean = false
) {
    // 导航项数据类
    data class NavigationItem(
        val title: String,
        val iconRes: Int,
        val route: String,
        val trailing: @Composable (() -> Unit)? = null
    )

    // 导航项目列表
    val navigationItems = listOf(
        NavigationItem("复制cookie", R.drawable.ic_copy, "copy_cookie"),
        NavigationItem("清除cookie", R.drawable.ic_delete, "clear_cookie"),
        NavigationItem("账号切换", R.drawable.ic_people, "account_management"),
        NavigationItem("测试XML解析", R.drawable.ic_setting, "test_xml_parse"),
        NavigationItem("调整字体大小", R.drawable.ic_setting, "font_size"),
        NavigationItem(
            title = if (isIncognitoEnabled) "关闭无痕模式" else "开启无痕模式", 
            iconRes = R.drawable.ic_setting, 
            route = "toggle_incognito"
        )
    )

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
        // 抽屉头部
        Surface(
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp),
            color = MaterialTheme.colorScheme.primary
        ) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(24.dp),
                verticalArrangement = Arrangement.Bottom
            ) {
                // 状态栏安全区域
                Spacer(
                    modifier = Modifier
                        .fillMaxWidth()
                        .windowInsetsPadding(WindowInsets.statusBars)
                        .height(0.dp)
                )

                // 用户头像
                UserAvatar(
                    userId = currentAccount?.userId,
                    size = 64.dp
                )

                Spacer(modifier = Modifier.height(8.dp))

                Text(
                    text = getName(currentAccount),
                    fontSize = AppFontSizes.largeBody,
                    color = MaterialTheme.colorScheme.onPrimary,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
            }
        }

        // 导航项目列表
        LazyColumn(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f),
            contentPadding = PaddingValues(vertical = 8.dp)
        ) {
            items(navigationItems) { item ->
                NavigationDrawerItem(
                    icon = {
                        Icon(
                            painter = painterResource(id = item.iconRes),
                            contentDescription = item.title,
                            modifier = Modifier.size(20.dp)
                        )
                    },
                    label = {
                        Text(
                            text = item.title,
                            fontSize = AppFontSizes.largeBody
                        )
                        item.trailing?.invoke()
                    },
                    selected = false,
                    onClick = { onItemClick(item.route) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 12.dp, vertical = 2.dp)
                        .clip(RoundedCornerShape(12.dp))
                )
            }
        }

        // 底部安全区域
        Spacer(
            modifier = Modifier
                .fillMaxWidth()
                .windowInsetsPadding(WindowInsets.navigationBars)
                .height(0.dp)
        )
    }
}

fun getName(currentAccount: UserAccount?): String {
    return if (currentAccount != null) {
        val result = "${currentAccount.username}(${currentAccount.userId})"
        result
    } else {
        "未知用户(未知ID)"
    }
}


